//
//  YGCacheManager.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/16.
//

#import "YGCacheManager.h"
NSString *const YGUserLoginCacheName = @"YGUserLoginCacheName";
static NSString *LoginUserInfoCacheKey = @"loginUserInfoCacheKey";

@interface YGCacheManager ()
@property (nonatomic, strong) YYDiskCache *userLoginCache;

@end
@implementation YGCacheManager

+ (instancetype)shareInstance {
    static id _instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    
    return _instance;
}

- (YYDiskCache *)checkCacheWithName:(NSString *)name{
    
    NSString *cacheFolder = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"YGCacheDir"];
    NSString *path = [cacheFolder stringByAppendingPathComponent:name];
    YYDiskCache *cache = [[YYDiskCache alloc] initWithPath:path inlineThreshold:NSUIntegerMax];
    [cache setAutoTrimInterval:60];
    [cache setCountLimit:10000];
    [cache trimToAge:NSIntegerMax];
    return cache;
}

- (YYDiskCache *)userLoginCache{
    if (!_userLoginCache) {
        _userLoginCache = [self checkCacheWithName:YGUserLoginCacheName];
    }
    return _userLoginCache;
}

- (void)saveLoginUserInfo:(NSArray *)infos{
    [self.userLoginCache setObject:infos forKey:LoginUserInfoCacheKey];
}

- (NSArray *)getUserInfos{
    NSArray *array = (NSArray *)[self.userLoginCache objectForKey:LoginUserInfoCacheKey];
    return  array;
}
@end
